﻿using MySql.Data.MySqlClient;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Init();

        }
        //数据删除整行
        public void Init()
        {
            MySqlConnection cn = new MySqlConnection("server=localhost;uid = root;database=data;charset=utf8");
            //2）适配器帮我们查数据  参数：1）查什么 2）数据连接管道
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter("select* from student", cn);
            //3)创建一个内存容器，创建一个空容器
            DataSet ds = new DataSet();
            //4)将数据从适配器中填入到容器中
            mySqlDataAdapter.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //使用ADO.net实现数据删除操作
            //e代表当前用户点击行的按钮
            if (e.CommandName == "delete")
            {
                int Did = Convert.ToInt32(e.CommandArgument);
                MySqlConnection connection = new MySqlConnection("server=localhost;uid = root;database=data;charset=utf8");
                string sql = string.Format("delete from student where id={0}", Did);
                MySqlCommand cmd = new MySqlCommand(sql, connection);

                //打开数据库
                connection.Open();
                int flag = cmd.ExecuteNonQuery();//执行并返回受影响行数
                connection.Close();
                if (flag == 1)
                {
                    Response.Write("<script>alert('删除成功');</script>");
                    Init();

                }
                

            }
            else
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("WebForm5.aspx?id=" + id);

            }
        }
    }
}